package com.furtherstar.auth.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.furtherstar.auth.controller.dto.UserCreator;
import com.furtherstar.auth.controller.dto.UserQuery;
import com.furtherstar.auth.controller.dto.UserRoleSetting;
import com.furtherstar.auth.dao.model.AuthUser;
import com.furtherstar.auth.dao.model.CorpWork;
import com.furtherstar.auth.dao.model.UserSecret;
import com.furtherstar.auth.service.AuthUserService;
import com.furtherstar.auth.service.UserRoleService;
import com.furtherstar.auth.service.WorkService;
import com.furtherstar.core.context.Work;
import com.furtherstar.core.context.WorkWithRoles;
import com.github.pagehelper.PageSerializable;
import com.yummy.convention.exception.AssertUtil;
import com.yummy.convention.http.Response;
import com.yummy.web.security.bean.SimpleUser;

@RestController
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	private AuthUserService authUserService;
	@Autowired
	private WorkService workService;
	@Autowired
	private UserRoleService userRoleService;

	@PostMapping("/add")
	public Response<Long> add(@RequestBody UserCreator userCreator){
		AssertUtil.isTrue(StringUtils.isNotBlank(userCreator.getUsername()),"用户名不能为空");
		AssertUtil.isTrue(StringUtils.isNotBlank(userCreator.getNickname()),"昵称不能为空");
		AssertUtil.isTrue(StringUtils.isNotBlank(userCreator.getPassword()),"密码不能为空");
		return Response.success(authUserService.add(userCreator));
	}

	@PostMapping("/update")
	public Response<Long> update(@RequestBody AuthUser authUser){
		AssertUtil.notNull(authUser.getId(),"id不能为空");
		return Response.success(authUserService.update(authUser));
	}

	@PostMapping("/changePassword")
	public Response<Boolean> changePassword(@RequestBody UserSecret userSecret){
		AssertUtil.isTrue(StringUtils.isNotBlank(userSecret.getPassword()),"密码不能为空");
		AssertUtil.notNull(userSecret.getUserid(),"userid不能为空");
		return Response.success(authUserService.changePassword(userSecret));
	}

	@PostMapping("/enable")
	public Response<Long> enable(@RequestBody AuthUser authUser){
		AssertUtil.notNull(authUser.getId(),"id不能为空");
		return Response.success(authUserService.enable(authUser.getId()));
	}

	@PostMapping("/disable")
	public Response<Long> disable(@RequestBody AuthUser authUser){
		AssertUtil.notNull(authUser.getId(),"id不能为空");
		return Response.success(authUserService.disable(authUser.getId()));
	}

	@PostMapping("/delete")
	public Response<Boolean> delete(@RequestBody AuthUser authUser){
		AssertUtil.notNull(authUser.getId(),"id不能为空");
		return Response.success(authUserService.delete(authUser.getId()));
	}

	@PostMapping("/page")
	public Response<PageSerializable<AuthUser>> page(@RequestBody UserQuery userQuery){
		return Response.success(authUserService.pageQuery(userQuery));
	}

	@PostMapping("/options")
	public Response<PageSerializable<SimpleUser>> options(@RequestBody UserQuery userQuery){
		return Response.success(authUserService.optionsQuery(userQuery));
	}

	@GetMapping("/workWithRoles/list")
	public Response<List<WorkWithRoles>> listWorkWithRoles(Long userid){
		AssertUtil.notNull(userid,"userid不能为空");
		return Response.success(workService.listUserWorkWithRoles(userid));
	}

	@PostMapping("/work/add")
	public Response<Long> addWork(@RequestBody Work work){
		AssertUtil.notNull(work.getUserid(),"userid不能为空");
		AssertUtil.notNull(work.getPositionId(),"positionId不能为空");
		AssertUtil.notNull(work.getCorpId(),"corpId不能为空");
		return Response.success(workService.add(work));
	}

	@PostMapping("/work/update")
	public Response<Long> updateWork(@RequestBody Work work){
		AssertUtil.notNull(work.getId(),"id不能为空");
		AssertUtil.notNull(work.getUserid(),"userid不能为空");
		AssertUtil.notNull(work.getPositionId(),"positionId不能为空");
		AssertUtil.notNull(work.getCorpId(),"corpId不能为空");
		return Response.success(workService.update(work));
	}

	@PostMapping("/work/delete")
	public Response<Boolean> deleteWork(@RequestBody CorpWork corpWork){
		AssertUtil.notNull(corpWork.getId(),"id不能为空");
		return Response.success(workService.delete(corpWork.getId()));
	}

	@PostMapping("/work/setRoles")
	public Response<Boolean> setUserRoles(@RequestBody UserRoleSetting userRoleSetting){
		AssertUtil.notNull(userRoleSetting.getWorkId(),"workId不能为空");
		AssertUtil.notNull(userRoleSetting.getRoleIds(),"roleIds不能为空");
		return Response.success(userRoleService.setUserRoles(userRoleSetting));
	}
}
